<?php
# settings
$_CELLSIZE=array(50,50);
$_DEPTH=50;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<meta http-equiv="Content-Style-Type" content="text/css">
		<meta http-equiv="Content-Script-Type" content="text/javascript">
		<style type="text/css">			
			#harta{
				position:relative;
				top:0px;
				left:0px;
				width: 2500px;
				height: 2500px;
				overflow:hidden;
			}
			#harta .tile{
				position:absolute;
				top:-200px;
				left:-200px;
				z-index:-50;
			}
			.city{
				position:relative;
				top:-25px;
				left:-25px;
			}
			.mountain{
				position:relative;
			
			}
			.forest{
				position:relative;
			}
		</style>
	</head>
	<body>
		<div id="map">
		<?php
			if($_POST) {
				$curCities = 0;
				$curMountains = 0;
				$curForests = 0;
				
				function randomTile($cities, $mountains, $forests) {
					GLOBAL $curMountains;
					GLOBAL $curForests;
					GLOBAL $curCities;

					$random = rand(0, 1000);
					if($random % 100 == rand(0, 20))
						if($curMountains < $mountains) {
							$curMountains++;
							return array('Tiles/0_nature_tile.jpg' , 1);
						}
					if($random % 100 == rand(0, 30))
						if($curForests < $forests) {
							$curForests++;
							return array('Tiles/1_nature_tile.jpg' , 1);
						}
					if($random % 100 == rand(0, 50))
						if($curCities < $cities) {
							$curCities++;
							return array('Tiles/city.png' , 2);
						}
					return array();
				}
				
				$cities = $_POST['cities'];
				$mountains = $_POST['mountains'];
				$forests = $_POST['forests'];
				
				$mapArray = array();
				for($i = 0; $i < 50; $i++)
					$mapArray[$i] = array();
				while($curMountains < $mountains && $curCities < $cities && $curForests < $forests) {
					for($i = 0; $i < 50; $i++) {
						for($j = 0; $j < 50; $j++) {
							if (empty($mapArray[$i][$j])) {
								if ($i == 0 && $j == 0)
									$mapArray[$i][$j] = array('Tiles/0_edge_tile.jpg' , 1);
								else if ($i == 0 && $j > 0 && $j < 49)
									$mapArray[$i][$j] = array('Tiles/1_edge_tile.jpg' , 1);
								else if ($i == 0 && $j == 49)
									$mapArray[$i][$j] = array('Tiles/2_edge_tile.jpg' , 1);
								else if ($i > 0 && $i < 49 && $j == 49)
									$mapArray[$i][$j] = array('Tiles/3_edge_tile.jpg' , 1);
								else if ($i == 49 && $j == 49)
									$mapArray[$i][$j] = array('Tiles/4_edge_tile.jpg' , 1);
								else if ($i == 49 && $j > 0 && $j < 49)
									$mapArray[$i][$j] = array('Tiles/5_edge_tile.jpg' , 1);
								else if ($i == 49 && $j == 0)
									$mapArray[$i][$j] = array('Tiles/6_edge_tile.jpg' , 1);
								else if ($i > 0 && $i < 49 && $j == 0)
									$mapArray[$i][$j] = array('Tiles/7_edge_tile.jpg' , 1);
								else
									$mapArray[$i][$j] = randomTile($cities, $mountains, $forests);
							}
						}
					}
				}
				$map = '<div id="harta">'."\n";
				for($i = 0; $i < 50; $i++) {
					for($j = 0; $j < 50; $j++) {
						if (empty($mapArray[$i][$j])) {
							# skipping
						} else {
							$tile=$mapArray[$i][$j];
							switch($tile[0]){
							case'Tiles/0_nature_tile.jpg':
								$map .= '<div class="tile" style="top: '.($_CELLSIZE[0] * $i).'px;left: '.($_CELLSIZE[1] * $j).'px;z-index: '.($_DEPTH * $tile[1]).'"><img src="'.$tile[0].'" class="mountain"></div>'."\n";
								break;
							case'Tiles/1_nature_tile.jpg':
								$map .= '<div class="tile" style="top: '.($_CELLSIZE[0] * $i).'px;left: '.($_CELLSIZE[1] * $j).'px;z-index: '.($_DEPTH * $tile[1]).'"><img src="'.$tile[0].'" class="forest"></div>'."\n";
								break;
							case'Tiles/city.png':
								$map .= '<div class="tile" style="top: '.($_CELLSIZE[0] * $i).'px;left: '.($_CELLSIZE[1] * $j).'px;z-index: '.($_DEPTH * $tile[1]).'"><img src="'.$tile[0].'" class="city"></div>'."\n";
								break;
							default:
								$map .= '<img class="tile" src="'.$tile[0].'" style="top: '.($_CELLSIZE[0] * $i).'px;left: '.($_CELLSIZE[1] * $j).'px;z-index: '.($_DEPTH * $tile[1]).'" />'."\n";
							}
						}
					}
				}
				$map .= '</div>'."\n";
				$map .= '<p>'.$curMountains.' '.$curForests.' '.$curCities.'</p>'."\n";
				echo $map;
			}
		?>
		</div>
		<div>
			<form action="index.php" method="post">
			Cities: <input type="text" name="cities" id="cities"><br>
			Mountains: <input type="text" name="mountains" id="mountains"><br>
			Forests: <input type="text" name="forests" id="forests"><br>
			<input type="submit" value="Create">
			</form>
		</div>
	</body>
</html>
